<?php

namespace app\api\controller\v1\posts;

use app\api\controller\v1\wechat\AuthController;
use app\Request;
use app\services\posts\PostCommentsServices;
use app\services\posts\PostsServices;

/**
 * 帖子评论
 */
class PostCommentsController extends AuthController
{
    /**
     * @var PostCommentsServices
     */
    protected $service;

    /**
     * PostCommentsServices constructor.
     * @param PostCommentsServices $services
     */
    public function __construct(PostCommentsServices $services)
    {
        $this->service = $services;
    }

    /**
     * 帖子评论
     * @param Request $request
     * @return mixed
     */
    public function comment(Request $request) {
        $data = $request->postMore([
            ['post_id', 0],
            ['content', ''],
            ['parent_id', 0]
        ]);
        $data['user_id'] = $request->uid();
        /** @var PostsServices $postsServices **/
        $postsServices = app()->make(PostsServices::class);
        $posts = $postsServices->getOne(['id' => $data['post_id']]);
        if (isset($posts) && $posts['status'] == 1) {
            $postsServices->update($posts['id'],['comment_count'=>$posts['comment_count'] + 1]);
            $this->service->save($data);
            return app("json")->success("评论成功");
        }

        return app("json")->fail("当前帖子无法评论");
    }

    /**
     * 帖子评论修改
     * @param Request $request
     * @return mixed
     */
    public function commentEdit(Request $request) {
        $data = $request->postMore([
            ['id', 0],
            ['content', '']
        ]);

        $comments = $this->service->getOne(['id' => $data['id']]);
        /** @var PostsServices $postsServices **/
        $postsServices = app()->make(PostsServices::class);
        $posts = $postsServices->getOne(['id' => $comments['post_id']]);
        if (isset($posts) && $posts['status'] == 1 && $posts['user_id'] == $request->uid()) {
            $this->service->update($data['id'], $data);
            return app("json")->success("评论修改成功");
        }
        return app("json")->fail("评论修改失败");
    }

    /***
     * 帖子评论删除
     * @param Request $request
     * @return mixed
     */
    public function delete(Request $request){
        $where = $request->getMore([
            ['id', 0],
        ]);
        /** @var PostsServices $postsServices **/
        $postsServices = app()->make(PostsServices::class);
        $comment = $this->service->getOne(['id' => $where['id']]);
        if (isset($comment)) {
            if ($comment['user_id'] == $request->uid()) {
                $this->service->delete($where['id']);
                return app("json")->success("删除评论成功");
            }
            $posts = $postsServices->getOne(['id' => $comment['post_id']]);
            if (isset($posts) && $posts['user_id'] == $request->uid()) {
                $this->service->delete($where['id']);
                return app("json")->success("删除评论成功");
            }
        }
        return app("json")->fail("删除评论失败");
    }

    /**
     * 帖子评论列表
     * @param Request $request
     * @return mixed
     */
    public function commentList(Request $request) {
        $where = $request->getMore([
            ['post_id', 0],
        ]);
        $list = $this->service->getList($where);
        return app("json")->success("查询成功", $list);
    }


}